﻿using System;
using System.Collections.Generic;

namespace ProblemsSet
{
    public class Problem_125 : BaseProblem
    {
        public override object GetResult()
        {
            const long max = 100000000;
            var sq = (Math.Sqrt(2*max-1)+1)/2;

            var hs = new HashSet<long>();
            long res = 0;

            for (long n = 2; n <= sq; n++)
            {
                long cn = n*(n + 1)*(2*n + 1);
                for (long k = 0; k <= n-2; k++)
                {
                    var tmp = (cn - k*(k + 1)*(2*k + 1))/6;
                    if (tmp >= max) continue;
                    if (hs.Contains(tmp))
                        continue;
                    if (!MathLogic.IsPolindrom(tmp)) continue;
                    hs.Add(tmp);
                    res += tmp;
                }
            }

            return res;
        }

        public override string Problem
        {
            get
            {
                return @"The palindromic number 595 is interesting because it can be written as the sum of consecutive squares: 62 + 72 + 82 + 92 + 102 + 112 + 122.

There are exactly eleven palindromes below one-thousand that can be written as consecutive square sums, and the sum of these palindromes is 4164. Note that 1 = 02 + 12 has not been included as this problem is concerned with the squares of positive integers.

Find the sum of all the numbers less than 108 that are both palindromic and can be written as the sum of consecutive squares.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 2906969179;
            }
        }

    }
}
